
/**
 * This class stores list of Products
 * @author Qi Qin
 *
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.Date;

public class CustomerList{
        private ArrayList <Customer> customerList;
        public CustomerList(){
        	customerList = new ArrayList<Customer>();
        }
       
        /**
         * Finds the sdmCompetitor of the list when given a number
         * @param number integer of the competitor number
         */
        public Customer findById(String id){
                //Loops through all competitors looking for given id number
                id = id.trim();
                int number = Integer.parseInt(id);
                for (Customer c: customerList){
                        if (Integer.parseInt(c.getID().trim()) == number){
                                //if number is found in list return that competitor
                                return c;
                        }
                }
                //if not return null
                return null;
        }
       
        /**
         * Adds an order to the array of orders
         * @param  order as an Order class object
         * @return boolean value, true if element is added
         */
        public boolean addCustomer(Customer c){
                String id = c.getID();
                Customer inList = this.findById(id);
                //Finds the number of the given competitor, should
                //that element not already exist in the list it is added
                if (inList == null){
                        customerList.add(c);
                        return true;
                }
                return false;
        }
       
        
        
        public String getDetailsOfCustomers()
        
        {
        	return null;
        }
        
        /**
         *
         */
        public void sortCustomers(){
               
        }
       
        /**
         *
         * @param filename
         */
        public void readCustomerListFile(String filename) {
                String inputLine = "";
                try {
                        File f = new File(filename);
                        Scanner scanner = new Scanner(f);
                        if (scanner.hasNextLine()){
                                inputLine = scanner.nextLine();
                        }
                        while (scanner.hasNextLine()) {
                                //loop while is another line to be read
                                inputLine = scanner.nextLine();        
                                ProcessLine(inputLine);
                        }
                }
                catch (FileNotFoundException fnf){
                        System.out.println("Error in CompetitorList.readFile "+ filename + " not found.");
                        System.exit(0);
                }
        }
        
        
        /**
         * Processes line, extracts data, creates SOKCompetitor object
         * and adds to list
         * Checks for non-numeric items when needed
         * @param line the line to be processed
         */
        private void ProcessLine(String line) {
                try {
                        String parts [] = line.split(",");//create array of String splitting line by comma
                        for(int i=0;i<parts.length;i++){
                                parts[i]=parts[i].trim();//delete extra spaces using trim
                        }
                        
                        String name=parts[0];
                        name.trim();
                        
                        String id=parts[1];
                        id.trim();
                     
                        String company_name=parts[2];
                        company_name.trim();
                        
                        int points = Integer.parseInt(parts[3]);
                        
                        String date_parts [] = line.split("/");//create array of String splitting line by comma
                        for(int i=0;i<date_parts.length;i++){
                        	date_parts[i]=date_parts[i].trim();//delete extra spaces using trim

                        String day=date_parts[0];
                        
                        String month=date_parts[1];
                        
                        String year=date_parts[2];
                        
                        String start_date=day+"/"+month+"/"+year;
                        
                        

                        Customer customer = new Customer(id, name, company_name, points,
                        		start_date);
                        this.addCustomer(customer);
                
                }}
                        
                        
                /**
                 * this catches trying to convert a String to an integer
                 */
                catch (NumberFormatException nfe) {
                        String error = "Number conversion error in '" + line + "'  - "
                                          + nfe.getMessage();
                        System.out.println(error);
                }
                /**
                 * this catches missing items if only one or two items
                 *other omissions will result in other errors
                 */
                catch (ArrayIndexOutOfBoundsException air) {
                        String error = "Error - Not enough items in  : '" + line
                                                + "' index position : " + air.getMessage();
                        System.out.println(error);
                }
        }
       
        public String getListOfAllCustomers(){
                String report = "ID CustomerId  ItemId  Quantity  OverAllCost\n";
                for (Customer c  : customerList){
                        report += String.format("%-4s", ""+c.getID());
                        report += String.format("%-13s", c.getName());
                        report += String.format("%-8.3s", c.getPoints());
                        report += String.format("%-10s",c.getStartDate());
                        //report += String.format("%-13s",c.getLoyalty());
                        report += "\n";
                }
                return report;
        }
        
        
        /**
         * Writes the list of all orders with calculated overallCost to an external text file
         * @param fileName string name of the file to be written to
         * @param text the string that will be written to the file
         */
        public void writeTo(String fileName, String text) {
                FileWriter filewriter;
                try {
                        filewriter = new FileWriter(fileName);
                        filewriter.write("LIST OF ALL CUSTOMERS:\n");
                        filewriter.write("\n");
                        filewriter.write(text);
                        filewriter.close();
                }
                //Catch error case where the file name is not found in location
                catch (FileNotFoundException fnf){
                        System.out.println(fileName + " not found ");
                        System.exit(0);
                }
                //Catch the IOException error
                catch (IOException ioe){
                        ioe.printStackTrace();
                        System.exit(1);
                }
        }
       
}




